package org.joshsacks.fishies;

import org.joshsacks.fishies.ui.gl.FishiesRenderer;
import org.joshsacks.fishies.ui.gl.GameBoardRenderer;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

public class Fishies extends Activity {
	private static final int TOUCH_CONTROL_MARGIN = 136;
  private static final int TOUCH_ICON_SIZE = 96;
  private GLSurfaceView gameSurfaceView;
	private FishiesController controller;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  controller = new FishiesController(this);
	  FishiesRenderer fishiesRenderer = new FishiesRenderer(this, controller);
	  controller.setRenderer(fishiesRenderer);
    gameSurfaceView = new GLSurfaceView(this);
    gameSurfaceView.setRenderer(fishiesRenderer);
    setContentView(gameSurfaceView);
	  gameSurfaceView.setOnTouchListener(new View.OnTouchListener() {         
	    public boolean onTouch(View v, MotionEvent event) {
	      int action = event.getAction();
        float x = event.getX();
        float y = event.getY();
        int width = v.getWidth();
        int height = v.getHeight();
        if (y < height - TOUCH_CONTROL_MARGIN) {
          return true;
        }

	      if (action == MotionEvent.ACTION_UP) {
	        controller.fastFall(false); 
	      } else if (action == MotionEvent.ACTION_DOWN) {
	        if (x < TOUCH_ICON_SIZE) {
	          controller.movePiece(false);                 
	        } else if (x > width - TOUCH_ICON_SIZE) {
	          controller.movePiece(true);
	        } else if (x > (width / 4) &&
	                x < (width / 4 + TOUCH_ICON_SIZE)) {
	          controller.rotatePiece(); 
	        } else if (x > (width * 3 / 4) - TOUCH_ICON_SIZE &&
                  x < (width * 3 / 4 )) {
	          System.err.println("Down: " + x);
            controller.fastFall(true); 
          }
	      }
	      return true;
	    }
	  });

	  gameSurfaceView.setOnKeyListener(new View.OnKeyListener() {
	    public boolean onKey(View v, int keyCode, KeyEvent event) {
	      System.err.println("kC: " + keyCode + " ac: " + event.getAction());
	      if (event.getAction() == KeyEvent.ACTION_DOWN) {
	        if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
	          controller.movePiece(false);                               
	        } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
	          controller.movePiece(true);                               
	        } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
	          controller.rotatePiece();                               
	        } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
	          controller.fastFall(true);                         
	        }
	        return true;
	      } else if (event.getAction() == KeyEvent.ACTION_UP) {
	        controller.fastFall(false);
	        return true;
	      }
	      return false;
	    }
	  });
	}

	@Override
	protected void onResume() {
	  super.onResume();
	  controller.onResume();
	  gameSurfaceView.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		controller.onPause();
		gameSurfaceView.onPause();
	}
}